import os
import sys
from typing import Optional

from PyQt5.QtWidgets import QWidget, QHBoxLayout, QLabel, QLineEdit, QPushButton, QFileDialog, QApplication


class PathSelectWidget(QWidget):
    __edtPath = None
    def __init__(self, parent: Optional[QWidget] = None):
        super().__init__(parent)
        self.setMaximumHeight(30)

        hl = QHBoxLayout()
        lb = QLabel('路径：')
        self.__edtPath = QLineEdit('')
        self.__edtPath.setReadOnly(True)
        self.__edtPath.setPlaceholderText('请选择路径')
        btn = QPushButton('...')
        btn.clicked.connect(self.__browse)
        hl.addWidget(lb)
        hl.addWidget(self.__edtPath)
        hl.addWidget(btn)
        hl.setContentsMargins(0,0,0,0)
        self.setLayout(hl)

    def __browse(self):
        directory = QFileDialog.getExistingDirectory(self, 'Open Folder', '/')
        if directory:
            self.__edtPath.setText(directory)

    def setPath(self, path: str):
        self.__edtPath.setText(path)

    def getPath(self) -> str:
        return self.__edtPath.text()

    def setPlaceholderText(self, text: str):
        self.__edtPath.setPlaceholderText(text)



if __name__ == '__main__':
    app = QApplication(sys.argv)
    try:
        current_path = os.path.abspath(__file__)
        path = os.path.dirname(current_path)
        path = os.path.dirname(path)
        app.setStyleSheet(open(f'{path}/styleSheet.qss', encoding='utf-8').read())
        window = PathSelectWidget()
        window.show()
        sys.exit(app.exec_())

    except Exception as e:
        print(e)

